package com.deli.springboot;

import com.deli.springboot.entity.Book;

import java.util.ArrayList;
import java.util.List;

public class RepeatList {

    public static void main(String[] args) {
        ArrayList<Book> books = new ArrayList<>();
        Book book1 = new Book();
        book1.setAuthor("cn");
        book1.setStatus(1000);
        Book book2 = new Book();
        book2.setAuthor("cn");
        book2.setStatus(500);
        Book book3 = new Book();
        book3.setAuthor("us");
        book3.setStatus(800);
        books.add(book1);
        books.add(book2);
        books.add(book3);
        repeat(books);
    }

    private static void repeat(List<Book> books) {
        ArrayList<Book> newBooks = new ArrayList<>();
        for(int i = 0;i<books.size();i++){//循环newBooks
            for(int j = i+1;j<books.size();j++){
                if(books.get(i).getAuthor().equals(books.get(j).getAuthor())){
                    Book book = books.get(i);
                    Integer status = book.getStatus();
                    status += books.get(j).getStatus();
                    book.setStatus(status);
                    newBooks.add(books.get(i));
                    books.remove(i);//删除一样的元素
                    i--;
                }else{
                    newBooks.add(books.get(j));
                }
            }
        }
       System.out.println(newBooks.toString());
    }
}
